package web;

import Utils.ReflectUtils;
import Utils.data.DataConverters;
import Utils.web.servlet.BaseServlet;
import Utils.web.servlet.ServletUtils;
import pojo.User;
import service.Impl.UserServiceImpl;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;

@WebServlet(name = "ManagerUserServlet", value = "/ManagerUserServlet")
public class ManagerUserServlet extends BaseServlet {
    private UserService userService = new UserServiceImpl();

    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        Integer id = Integer.valueOf(request.getParameter("id"));
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String typeStr = request.getParameter("type");
        Integer type = "管理员".equals(typeStr) ? 1 : 0;
        User user = userService.queryById(id);
        user.setUsername(username);
        user.setPassword(password);
        user.setType(type);
        userService.update(user);
        int pn = DataConverters.parseInt(request.getParameter("pn"), 1);
        response.sendRedirect(request.getContextPath() + "/ManagerUserServlet?action=page&pn=" + pn);
    }

    protected void addRedirect(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("action", "add");
        request.getRequestDispatcher("/pages/manager/edit_user.jsp").forward(request, response);
    }

    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String typeStr = request.getParameter("type");
        int pt = DataConverters.parseInt(request.getParameter("pt"), 1);
        Integer type = "管理员".equals(typeStr) ? 1 : 0;
        User user = new User(null, username, password, type);
        userService.register(user);
        response.sendRedirect(request.getContextPath() + "/ManagerUserServlet?action=page");
    }

    protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        ServletUtils.page(request,
                response,
                "ManagerUserServlet?action=page",
                "pages/manager/user_manager.jsp",
                userService);
    }

    protected void pageBlur(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException, InvocationTargetException, IllegalAccessException {
        String username = request.getParameter("username");
        request.setAttribute("username", username);
        ServletUtils.page(request,
                response,
                "ManagerUserServlet?action=pageBlur",
                "pages/manager/user_manager.jsp",
                userService,
                ReflectUtils.getMethod(userService, "getPageByUsernameBlur", Integer.class, Integer.class, String.class, String.class),
                username);
    }

    protected void queryById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        Integer id = Integer.valueOf(request.getParameter("id"));
        User user = userService.queryById(id);
        request.setAttribute("user", user);
        request.setAttribute("action", "update");
        request.getRequestDispatcher("/pages/manager/edit_user.jsp").forward(request, response);
    }
}
